package cn.yh.shop.user.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.yh.shop.user.model.User;
import cn.yh.shop.user.model.UserAddress;
import cn.yh.shop.user.service.IAddressService;
/**
 * @Description:收货地址控制层
 * @author:仇文文
 * @time:2016年12月15日 上午9:12:44
 */
public class AddressAction extends ActionSupport implements ModelDriven<Object> {
	private static Logger logger = LogManager.getLogger(AddressAction.class);
	// 日志
	/**r
	 * @Description: serialVersionUID : 序列化
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @Description: addressService :自动注入addressService
	 */
	@Autowired
	private IAddressService addressService;
	/**
	 * @Description: UserAddress对象
	 */
	private UserAddress userAddress;
	private User user;

	/**
	 * @Description:UserAddress对象的泛型集合，地址存放在list中
	 */
	private Map<String,List<UserAddress>> addressMap=new HashMap<String,List<UserAddress>>();
	
	public void setAddressMap(Map<String, List<UserAddress>> addressMap) {
		this.addressMap = addressMap;
	}
	
	public Map<String, List<UserAddress>> getAddressMap() {
		return addressMap;
	}

	public UserAddress getUserAddress() {
		return userAddress;
	}
	public void setUserAddress(UserAddress userAddress) {
		this.userAddress = userAddress;
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public Object  getModel() {
		// TODO Auto-generated method stub
		if (userAddress == null) {
			userAddress = new UserAddress();
		}
		return userAddress;
	}
	
	/**
	 * @Description:获取用户收货地址
	 * @return
	 * String
	 * @exception:
	 * @author: 仇文文
	 * @time:2016年12月21日 下午2:41:28
	 */
	public String showAllAddress(){
		User user = (User)ActionContext.getContext().getSession().get("data");
		//获取登陆user的session值
		logger.info(user);
		List<UserAddress> showAllAddress = addressService.showAllAddress(user);
		//返回所以收货地址，存放在List集合中
		addressMap.put("result", showAllAddress);
		//用json传值
		ActionContext.getContext().getSession().put("data",user );
		//返回user的session值
		ActionContext.getContext().getSession().put("address",addressMap );
		//返回addressMap的session值
		logger.info("显示地址");
		return "success";
		
	}
	/**
	 * @Description:添加收货地址
	 * @return
	 * String
	 * @exception:
	 * @author: 仇文文
	 * @time:2016年12月22日 下午4:47:40
	 */
	public String addAddress(){
		addressService.addAddress(userAddress);
		logger.info("地址："+userAddress);
		logger.info("成功添加收货地址");
		return "success";
		
	}

	
}
